﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;

namespace Project_2._2.Controllers
{
    public class LoaiXeController : Controller
    {
        //
        // GET: /LoaiXe/

        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var lx = (from p in db.LoaiXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                        ViewBag.record = rd;
                        return View(lx);
                    }
                    else
                    {
                        var lx = (from p in db.LoaiXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, 3);
                        return View(lx);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            int record = 0;
            if (string.IsNullOrEmpty(col["KeyWord"]))
            {
                record = int.Parse(col["Record"]);
                var lx = (from p in db.LoaiXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                ViewBag.record = col["Record"];
                return View(lx);
            }
            else
            {
                string SearchBy = col["SearchBy"].ToString();
                string key = col["KeyWord"].ToString();

                switch (SearchBy)
                {
                    case "TenLoaiXe":
                        var lx = (from p in db.LoaiXes where p.isDelete == false & p.TenLoaiXe.Contains(key) select p).ToList().ToPagedList(page ?? 1, 1);
                        ViewBag.record = "1";
                        return View(lx); break;
                }
                return View();
            }

        }

        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> multi_delete_loaixe)
        {
            var lx_delete = db.LoaiXes.Where(x => multi_delete_loaixe.Contains(x.LoaiXeId)).ToList();
            foreach (var lx in lx_delete)
            {
                lx.isDelete = true;
                UpdateModel(lx);
                db.SaveChanges();
            }
            return RedirectToAction("Default", "LoaiXe");
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(LoaiXe_ViewModels lx)
        {
            if (ModelState.IsValid)
            {
                if (lx.TenLoaiXe == null)
                {
                    ModelState.AddModelError("", "Tên loại xe không được để trống...!");
                }
                else
                {
                    LoaiXe loaixe = new LoaiXe();
                    loaixe.TenLoaiXe = lx.TenLoaiXe;
                    loaixe.MoTa = lx.MoTa;
                    loaixe.isDelete = false;
                    db.LoaiXes.Add(loaixe);
                    db.SaveChanges();
                    ModelState.Clear();
                    lx = new LoaiXe_ViewModels();
                    ModelState.AddModelError("", "Đã thêm thành công...!");
                }
            }
            return View(lx);
        }
        public ActionResult Delete(int id)
        {
            LoaiXe loaixe = (from p in db.LoaiXes where p.LoaiXeId == id select p).First();
            loaixe.isDelete = true;
            UpdateModel(loaixe);
            db.SaveChanges();
            return RedirectToAction("Default", "LoaiXe");
        }
        public ActionResult Update(int id)
        {
            var lx = (from p in db.LoaiXes where p.LoaiXeId == id select p).First();
            return View(lx);
        }
        [HttpPost]
        public ActionResult Update(int id, FormCollection col)
        {
            var lx = (from p in db.LoaiXes where p.LoaiXeId == id select p).First();
            lx.TenLoaiXe = col["TenLoaiXe"];
            lx.MoTa = col["MoTa"];
            UpdateModel(lx);
            db.SaveChanges();
            ModelState.AddModelError("", "Cập nhật thành công.");
            return RedirectToAction("Default");
        }

    }
}
